#pragma once

#include<iostream>
#include<string>
#include<any>
#include<memory>
#include<jsoncpp/json/json.h>
#include "ConnectionPool.hpp"


class Result
{
public:
    Result(const std::shared_ptr<MYSQL_RES>& res)
    {
       MYSQL_FIELD* fields = mysql_fetch_fields(res.get());
       int n = mysql_num_fields(res.get());
       for(int i = 0;i<n;++i)
       {
            std::string name = fields[i].name;
            _names.push_back(std::move(name)); 
       }
       //按行获取数据
       MYSQL_ROW row_data = nullptr;
       while((row_data = mysql_fetch_row(res.get())))
       {
             Json::Value field;
             for(int i = 0;i<_names.size();++i) 
             {
                field[_names[i]] = row_data[i];
             }
             _root.append(field);
       }
    }
    const Json::Value& result() const
    {
        return _root;
    }
private:
    Json::Value _root;
    std::vector<std::string> _names;
};



